/**
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 * @data:2017年10月9日
 * @author:MaShibo
 */
package com.wttech.dzzf.process.service.issuer;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import com.wttech.dzzf.process.dao.ProcessIssuerInfoDao;
import com.wttech.dzzf.process.model.Organization;
import com.wttech.dzzf.process.model.issuer.Agency;
import com.wttech.dzzf.process.model.issuer.Hall;
import com.wttech.dzzf.process.model.issuer.Issuer;
import com.wttech.dzzf.process.model.issuer.MobileService;
import com.wttech.dzzf.process.model.issuer.Online;
import com.wttech.dzzf.process.model.issuer.Terminal;

/**
 * 发行服务设施相关数据编码/生成
 * 
 * @date 2017年10月9日
 * @author MaShibo
 */
@Service
public class IssuerInfosCreatorService {
    
    
    private static final Logger LOGGER = LoggerFactory.getLogger(IssuerInfosCreatorService.class);
    
    private static final String DEFAUL_JT_AGENCYID = "61010199999";
    
    private ProcessIssuerInfoDao processIssuerInfoDao;
    
    @Autowired
    public IssuerInfosCreatorService(ProcessIssuerInfoDao processIssuerInfoDao) {
        this.processIssuerInfoDao = processIssuerInfoDao;
    }
    
    // 发行方信息
    // ================================================
    public void createIssuerInfo() {
        // 仅有一个发行方
        Issuer issuer = new Issuer();
        issuer.setId("610101");
        issuer.setName("陕西发行");
        issuer.setContact("聂宏海");
        issuer.setTel("029-12122");
        issuer.setAddress("陕西省西安市未央区未央路208号");
        issuer.setStartTime("2015-08-17");
        processIssuerInfoDao.createValueObject(issuer);
    }
    
    // 客服合作机构信息
    public void createAgencyInfos() {
        List<Agency> infos = processIssuerInfoDao.queryAgencyInfos();
        infos.stream().forEach(info -> createAgencyInfo(info));
    }
    
    private void createAgencyInfo(Agency info) {
        try {
            processIssuerInfoDao.createValueObject(info);
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
    }
    
    // ~服务网点信息
    // ================================================
    
    private void buildAndCreateHallInfos() {
        // 查询待处理的服务网点信息
        processIssuerInfoDao.resetHallSeqNo();
        List<Hall> infos = processIssuerInfoDao.queryHallInfos();
        LOGGER.info("开始编码并创建服务网点记录..");
        infos.forEach(this::createHallInfo);
        LOGGER.info("服务网点创建完成");
    }
    
    private void createHallInfo(Hall hall) {
        if ((StringUtils.isBlank(hall.getAgencyId()) && hall.getOrgType() != null && hall.getOrgType().equals(4))
                || (hall.getOrgType() != null && hall.getOrgType().equals(3)&&hall.getTopClass().equals("6101"))) {
            hall.setAgencyId(DEFAUL_JT_AGENCYID);
        }
        Integer maxSeqNo = processIssuerInfoDao.queryMaxCountySeqNo(hall.getAgencyId() + hall.getCityId() + hall.getCountyId());
        int seqNo = maxSeqNo + 1;
        String id = hall.getAgencyId() + hall.getCityId() + hall.getCountyId() +
                generateHallSeqNo(String.valueOf(seqNo));
        hall.setId(id);
        
        try {
            Organization org = new Organization();
            org.setOrgId(hall.getOrgId());
            org.setHallSeqNo(seqNo);
            org.setChannelId(id);
            org.setAgencyType(11);
            processIssuerInfoDao.updateObject(org);
            
            processIssuerInfoDao.createValueObject(hall);
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
    }
    
    private String generateHallSeqNo(String seqNo) {
        while (StringUtils.length(seqNo) < 4) {
            seqNo = "0" + seqNo;
        }
        return seqNo;
    }
    
    // ~流动服务网点信息
    // ================================================
    
    public void createMobilServiceInfo() {
        try {
            MobileService ms = new MobileService();
            ms.setId("610101999990001");
            ms.setStartTime("2015-08-17");
            processIssuerInfoDao.createValueObject(ms);
            
            Organization org = new Organization();
            org.setOrgId("57B21B75-D7C6-C4FC-424D-38737D0D9834");
            org.setChannelId(ms.getId());
            org.setStartOn(DateUtils.parseDate(ms.getStartTime(),
                    new String[] { DateFormatUtils.ISO_DATE_FORMAT.getPattern() }));
            org.setEndOn(DateUtils.parseDate(ms.getEndTime(),
                    new String[] { DateFormatUtils.ISO_DATE_FORMAT.getPattern() }));
            processIssuerInfoDao.createValueObject(org);
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
    }
    
    // ~ 自助服务终端
    // ================================================
    
    public void createTerminalInfos() {
        
        // 自助圈存机
        Terminal terminal = new Terminal();
        terminal.setId("6101019999901120001");
        terminal.setServiceItems("25");
        terminal.setBusinessHours("每日0:00-24:00");
        terminal.setAddress("陕西省西安市未央区未央路208号未央营业厅");
        terminal.setStartTime("2016-01-01");
        terminal.setEndTime("2999-12-31");
        processIssuerInfoDao.createValueObject(terminal);
        terminal.setId("6101019999901120002");
        processIssuerInfoDao.createValueObject(terminal);
        
        // 西安银行ATM
        terminal.setId("6101010203201120001");
        processIssuerInfoDao.createValueObject(terminal);
        
        // 农行自助充值
        terminal.setId("6101010200401120001");
        processIssuerInfoDao.createValueObject(terminal);
        
        // 长安银行自助充值
        terminal.setId("6101010203101120001");
        processIssuerInfoDao.createValueObject(terminal);
    }
    
    // ~ 线上服务渠道
    // ================================================
    
    public void createOnlineInfos() {
        Online online = new Online();
        online.setId("610101999990202");
        online.setName("邮政储蓄银行");
        online.setServiceItems("25");
        online.setStartTime("2016-01-01");
        online.setEndTime("2999-12-31");
        processIssuerInfoDao.createValueObject(online);
        
        online.setId("610101999990203");
        online.setName("中国建设银行");
        processIssuerInfoDao.createValueObject(online);
        
        online.setId("610101999990204");
        online.setName("中国工商银行");
        processIssuerInfoDao.createValueObject(online);
        
        online.setId("610101999990205");
        online.setName("中国银行");
        processIssuerInfoDao.createValueObject(online);
        
        online.setId("610101999990206");
        online.setName("中国农业银行");
        processIssuerInfoDao.createValueObject(online);
        
    }
    
    public static void main(String[] args) {
        @SuppressWarnings("resource")
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        IssuerInfosCreatorService service = context.getBean(IssuerInfosCreatorService.class);
        // service.createIssuerInfo();
        // service.createAgencyInfos();
        service.buildAndCreateHallInfos();
        // service.createMobilServiceInfo();
        // service.createTerminalInfos();
        // service.createOnlineInfos();
    }
}
